package com.yy.crypto;

import cn.hutool.core.codec.Base64;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;

/**
 * @author lyy
 * @date 2021/12/19
 */
public class RSAHutoolUtil {
    public static void main(String[] args) {
        String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDYWlhcpxBNhH7tJd2DpT5KgYEa" +
                "hTyuULumtGOIxDdkvmmKD4GbNMvrXyrYWrcHt9nDoskrjZF47FfEzLbhKm+TtzOc" +
                "svU7ZZy2pfBLdme2vW1Gt6VLWw5OOUe2o0HqNOzK6k54ixJ3xuzoiFLixMvb9InW" +
                "6kHNl1ng8/30U9AMWQIDAQAB";
        String privateKey = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBANhaWFynEE2Efu0l" +
                "3YOlPkqBgRqFPK5Qu6a0Y4jEN2S+aYoPgZs0y+tfKthatwe32cOiySuNkXjsV8TM" +
                "tuEqb5O3M5yy9TtlnLal8Et2Z7a9bUa3pUtbDk45R7ajQeo07MrqTniLEnfG7OiI" +
                "UuLEy9v0idbqQc2XWeDz/fRT0AxZAgMBAAECgYEAo8H2YeLd9x6Th+KCQTOg63oM" +
                "g0tX+8UF1A9zy0BhgTKS5boI6rZSpww4fNxk70WcYXTkeb/E3Vdes4oT3mhlXHAm" +
                "TWFQ9T6SvUFjA1Pq9r8uijb01BXwgqJyZ2CqV8cTP2sZtQqLn0mU5Xk+lnn4hL9n" +
                "secWv5xNEfXypdLJvYUCQQDtGjfwiiCdaMGu+za5mn8ifgsrjF54AS/8bc41STqZ" +
                "iR4ZWCIBNYY7p8OJawyWnpof5iMscPU/MTgR36W6mBQHAkEA6ZjCN8HOzWz9THWo" +
                "bHJqSdOMVigiSAyydy7lei2XhlFbYKSCFXQL7MGPYQ2Fo3XhhqCBieKAa4oGzyeS" +
                "YLiEnwJAIpJ0HarGXq1rsGwI6r9QUmM2lb7991mU+aW5L6Ph24lWzjM/ohV/4Srx" +
                "JBRr3Xp61rPj5RMDb5dpqZGGxNJv+QJBAISxjcH2ie8PdmO0glP4OoBtd06Jxc9/" +
                "xvEE5r4cn/JgzLN9P5fIL+3xlezfrYZJNC8xheQ5khGEepG6POBMcKkCQEPkpcbz" +
                "drTPvlXIHK5errRsWZVGLm2JTOxS6zW/dGuA0x2IXseEhEJBXyu3d2A9UIRCZtJb" +
                "ADcKyVszfwYsUsY=";
        String content = "bHJqSdOMVigiSAyydy7lei2XhlFbYKSCFXQL7MGPYQ2Fo3XhhqCBieKAa4oGzyeSbHJqSdOMVigiSAyydy7lei2XhlFbYKSCFXQL7MGPYQ2Fo3XhhqCBieKAa4oGzyeSbHJqSdOMVigiSAyydy7lei2XhlFbYKSCFXQL7MGPYQ2Fo3XhhqCBieKAa4oGzyeSbHJqSdOMVigiSAyydy7lei2XhlFbYKSCFXQL7MGPYQ2Fo3XhhqCBieKAa4oGzyeSbHJqSdOMVigiSAyydy7lei2XhlFbYKSCFXQL7MGPYQ2Fo3XhhqCBieKAa4oGzyeS";
        String encrypt = encrypt(content, publicKey);
        System.out.println(encrypt);
        System.out.println(decrypt(encrypt, privateKey));
    }

    public static String encrypt(String content, String publicKey){
        RSA rsa = new RSA(null, publicKey);
        return rsa.encryptBase64(content, KeyType.PublicKey);
    }

    public static String decrypt(String content, String privateKey){
        RSA rsa = new RSA(privateKey, null);
        byte[] str = Base64.decode(content);
        return new String(rsa.decrypt(str, KeyType.PrivateKey));
    }
}
